package com.deer.wms.ware.task.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.deer.wms.base.system.service.CellInfoService;
import com.deer.wms.base.system.service.ErpService;
import com.deer.wms.base.system.service.ItemInfoService;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.system.manage.dao.WareInfoMapper;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.model.pushErp.StrategyErpService;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.out.OutDetail;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import com.deer.wms.ware.task.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author: xu
 * @Date: 2022/06/15/13:42
 * @Description:
 */
@Service
public class AsyncServiceImpl implements AsyncService {
    @Autowired
    private SoMasterServiceImpl soMasterService;
    @Autowired
    private OutMasterService outMasterService;
    @Autowired
    private WaveDetailServiceImpl waveDetailService;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private PushErpService pushErpService;
    @Autowired
    private CellInfoService cellInfoService;
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private ItemInfoService itemInfoService;
    @Autowired
    private SoDetailService soDetailService;
    @Autowired
    private StrategyErpService strategyErpService;
    @Autowired
    private ErpService erpService;
    @Autowired
    private WareInfoMapper wareInfoMapper;





    @Transactional()
    public Map<String, String> asyncPushErp(List<Inventory> inventories, Set<OutDetail> outDetailSet, CurrentUser currentUser) {
        System.out.println(Thread.currentThread().getName());
        //Step1.生成出库单
        OutDetail soDetail = outDetailSet.iterator().next();
        SoMaster soMaster = soMasterService.getOneByField("bill_no", soDetail.getBillNo());
        //Step2.处理库存，生成库存事务
        for (Inventory inventory : inventories) {
            waveDetailService.toStockRequest(inventory, currentUser);
            inventoryService.removeById(inventory);
        }
        return null;
    }


    @Override
    @Transactional()
    public Map<String, String> asyncReview(SoMaster soMaster, String cellCode, Set<OutDetail> outDetails, CurrentUser currentUser, String pickIds) {
        Map<String, String> map = new HashMap<>();
        outMasterService.createOutBill(soMaster, outDetails, currentUser);
        Integer erpAreaId =  cellInfoService.findbyCellCode(cellCode);
        BaseAssist baseAssist = new BaseAssist();
        baseAssist.setStockId2(erpAreaId);
        return null;
    }


}
